package com.huey.utils.execl;

import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import cn.afterturn.easypoi.excel.entity.params.ExcelForEachParams;
import cn.afterturn.easypoi.excel.export.styler.IExcelExportStyler;
import org.apache.poi.ss.usermodel.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author xubo
 * @version 1.0
 * @description 默认样式工具类
 * @createTime 2019/12/26 11:13
 */
public class ExcelStyleUtil implements IExcelExportStyler {
    private static final short FONT_SIZE_TWELVE = 12;
    /**
     * 大标题样式
     */
    private CellStyle headerStyle;
    /**
     * 每列标题样式
     */
    private CellStyle titleStyle;
    /**
     * 数据行样式
     */
    private CellStyle styles;

    public ExcelStyleUtil(Workbook workbook) {
        this.init(workbook);
    }

    /**
     * 初始化样式
     *
     * @param workbook
     */
    private void init(Workbook workbook) {
        this.headerStyle = initHeaderStyle(workbook);
        this.titleStyle =  ExcelStyleUtil.getCellStyleMap(workbook).get(ExcelCellStyle.HEADER.name());
        this.styles = ExcelStyleUtil.getCellStyleMap(workbook).get(ExcelCellStyle.DATA.name());
    }

    /**
     * 大标题样式
     *
     * @param color
     * @return
     */
    @Override
    public CellStyle getHeaderStyle(short color) {
        return headerStyle;
    }

    /**
     * 每列标题样式
     *
     * @param color
     * @return
     */
    @Override
    public CellStyle getTitleStyle(short color) {
        return titleStyle;
    }

    /**
     * 数据行样式
     *
     * @param parity 可以用来表示奇偶行
     * @param entity 数据内容
     * @return 样式
     */
    @Override
    public CellStyle getStyles(boolean parity, ExcelExportEntity entity) {
        return styles;
    }

    /**
     * 获取样式方法
     *
     * @param dataRow 数据行
     * @param obj     对象
     * @param data    数据
     */
    @Override
    public CellStyle getStyles(Cell cell, int dataRow, ExcelExportEntity entity, Object obj, Object data) {
        return getStyles(true, entity);
    }

    /**
     * 模板使用的样式设置
     */
    @Override
    public CellStyle getTemplateStyles(boolean isSingle, ExcelForEachParams excelForEachParams) {
        return null;
    }

    /**
     * 初始化--大标题样式
     *
     * @param workbook
     * @return
     */
    private CellStyle initHeaderStyle(Workbook workbook) {
        CellStyle style = getBaseCellStyle(workbook);
        style.setFont(getFont(workbook, FONT_SIZE_TWELVE, true));
        return style;
    }

    /**
     * 基础样式
     *
     * @return
     */
    private CellStyle getBaseCellStyle(Workbook workbook) {
        CellStyle style = workbook.createCellStyle();
        //下边框
        style.setBorderBottom(BorderStyle.THIN);
        //左边框
        style.setBorderLeft(BorderStyle.THIN);
        //上边框
        style.setBorderTop(BorderStyle.THIN);
        //右边框
        style.setBorderRight(BorderStyle.THIN);
        //水平居中
        style.setAlignment(HorizontalAlignment.CENTER);
        //上下居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        //设置自动换行
        style.setWrapText(true);
        return style;
    }

    /**
     * 原叶子基础样式
     * @param workbook
     * @return
     */
    public static Map<String, CellStyle> getCellStyleMap(Workbook workbook) {
        Map<String, CellStyle> map = new HashMap<>();
        CellStyle cellStyleHeader = workbook.createCellStyle();
        cellStyleHeader.setWrapText(true);
        cellStyleHeader.setAlignment(HorizontalAlignment.LEFT);
        cellStyleHeader.setBorderRight(BorderStyle.THIN);
        cellStyleHeader.setRightBorderColor(IndexedColors.BLACK.getIndex());
        cellStyleHeader.setBorderLeft(BorderStyle.THIN);
        cellStyleHeader.setLeftBorderColor(IndexedColors.BLACK.getIndex());
        cellStyleHeader.setBorderTop(BorderStyle.THIN);
        cellStyleHeader.setTopBorderColor(IndexedColors.BLACK.getIndex());
        cellStyleHeader.setBorderBottom(BorderStyle.THIN);
        cellStyleHeader.setBottomBorderColor(IndexedColors.BLACK.getIndex());
        cellStyleHeader.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
        cellStyleHeader.setFillPattern(FillPatternType.SOLID_FOREGROUND);

        //水平居中
        cellStyleHeader.setAlignment(HorizontalAlignment.CENTER);
        //上下居中
        cellStyleHeader.setVerticalAlignment(VerticalAlignment.CENTER);

        Font headerFont = workbook.createFont();
        headerFont.setFontName("微软雅黑");
        headerFont.setFontHeightInPoints((short) 10);
        headerFont.setBold(true);
        headerFont.setColor(IndexedColors.WHITE.getIndex());
        cellStyleHeader.setFont(headerFont);
        map.put(ExcelCellStyle.HEADER.name(), cellStyleHeader);

        CellStyle cellStyleData = workbook.createCellStyle();
        cellStyleData.setWrapText(true);
        cellStyleData.setAlignment(HorizontalAlignment.LEFT);
        cellStyleData.setBorderRight(BorderStyle.THIN);
        cellStyleData.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
        cellStyleData.setBorderLeft(BorderStyle.THIN);
        cellStyleData.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
        cellStyleData.setBorderTop(BorderStyle.THIN);
        cellStyleData.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
        cellStyleData.setBorderBottom(BorderStyle.THIN);
        cellStyleData.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
        Font dataFont = workbook.createFont();
        dataFont.setFontName("微软雅黑");
        dataFont.setFontHeightInPoints((short) 10);
        cellStyleData.setFont(dataFont);
        map.put(ExcelCellStyle.DATA.name(), cellStyleData);

        CellStyle cellStyleYellow = workbook.createCellStyle();
        cellStyleYellow.cloneStyleFrom(cellStyleData);
        cellStyleYellow.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
        cellStyleYellow.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        map.put(ExcelCellStyle.YELLOW.name(), cellStyleYellow);

        CellStyle cellStyleLightGreen = workbook.createCellStyle();
        cellStyleLightGreen.cloneStyleFrom(cellStyleData);
        cellStyleLightGreen.setFillForegroundColor(IndexedColors.LIGHT_GREEN.getIndex());
        cellStyleLightGreen.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        map.put(ExcelCellStyle.LIGHT_GREEN.name(), cellStyleLightGreen);

        CellStyle cellStyleRed = workbook.createCellStyle();
        cellStyleRed.cloneStyleFrom(cellStyleData);
        cellStyleRed.setFillForegroundColor(IndexedColors.RED.getIndex());
        cellStyleRed.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        map.put(ExcelCellStyle.RED.name(), cellStyleRed);

        CellStyle cellStyleLightBlue = workbook.createCellStyle();
        cellStyleLightBlue.cloneStyleFrom(cellStyleData);
        cellStyleLightBlue.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());
        cellStyleLightBlue.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        map.put(ExcelCellStyle.LIGHT_BLUE.name(), cellStyleLightBlue);
        return map;
    }

    /**
     * 字体样式
     *
     * @param size   字体大小
     * @param isBold 是否加粗
     * @return
     */
    private Font getFont(Workbook workbook, short size, boolean isBold) {
        Font font = workbook.createFont();
        //字体样式
        font.setFontName("宋体");
        //是否加粗
        font.setBold(isBold);
        //字体大小
        font.setFontHeightInPoints(size);
        return font;
    }

    enum ExcelCellStyle {
        HEADER,DATA,YELLOW,LIGHT_GREEN,RED,LIGHT_BLUE
    }
}


